/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.export.Savable;
import com.jme3.material.Material;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;

/**
 *
 * @author tipp1san
 */


public class PlayerShipControl extends AbstractControl implements Savable, Cloneable {

    private float angle;
    private Node ship;
    private ShipFactory shipControl;
    private BulletAppState bas;
    private SimpleApplication sa;
    private AssetManager am;
    private Bullet bullet;
    private boolean doMove = false;
    private boolean doRotate = true;
    private float rotateSpeed;
    private Camera cam;
            
    public PlayerShipControl(Camera cam, Node ship, BulletAppState bas, AssetManager am, SimpleApplication sa){
        this.cam = cam;
        this.ship = ship;
        this.bas = bas;
        this.am = am;
        this.sa = sa;
        
        setShip();
        
        shipControl.setMoveSpeed(60f);
        rotateSpeed = 50f;
        
        //wepaonControl = new bullet(sm, ship);
        //ship.addControl(weaponControl);
    }
    
    private void setShip() {
        Spatial shipSpatial = am.loadModel("Models/ship1/ship1.mesh.j3o") ;
        Material ship_mat = (Material) am.loadMaterial("Materials/ship1_mat.j3m");
        shipSpatial.setMaterial(ship_mat);
        shipSpatial.scale(0.005f, 0.005f, 0.005f);
        
        shipSpatial.setUserData("Type", "Player");
        
        ship.attachChild(shipSpatial);
        ship.setUserData("Type", "Player");
        
        CollisionShape collisionShape = new BoxCollisionShape(new Vector3f(1.0f, 1.0f, 1.0f));
        collisionShape.setMargin(0.05f);
        shipControl = new ShipFactory(collisionShape, 1, bas);
        
        shipControl.setDamping(0.75f, 0.9999f);
        shipControl.setFriction(0.2f);
        ship.addControl(shipControl);
        bas.getPhysicsSpace().add(shipControl);
        shipControl.setEnabled(true);
        shipControl.setGravity(Vector3f.ZERO);
    }
    
    void makeMove(boolean boo){
        if (boo) doMove = true;
        else doMove = false;
    }
        
    void makeRotate(boolean boo){
        if (boo) doRotate = true;
        else doRotate = false;
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        if (doMove){
            //shipControl.setFlyDirection(Vector3f.ZERO);
        }
        if (doRotate){
            //shipControl.setViewDirection(Quaternion.IDENTITY);
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {

    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
 